package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import service.tDeviceService;
import service.tUserAndtDeviceService;
import service.tUserService;
import com.opensymphony.xwork2.ActionContext;
import entity.tDevice;
import entity.tUser;
import entity.tUserAndtDevice;

public class AllotDeviceAction {
	
	private String deviceNo;
	private String deviceName;
	private String userName;
	private String deviceDescribe;
	
	private tUserService tuserService;
	private tDeviceService tdeviceService;
	private tUserAndtDeviceService uadservice;
	
	public String allot() throws IOException, ParseException{
		
		ActionContext context = ActionContext.getContext();  
	    HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);  
	    //HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);  
	    @SuppressWarnings("unused")
		Map session = context.getSession(); 
		ServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		@SuppressWarnings("unused")
		PrintWriter out = response.getWriter();
		System.out.println("----------经过AllotDeviceAction----------");
		
		System.out.println("deviceNo:"+deviceNo+",deviceName:"+deviceName+",userName:"+userName+",deviceDescribe:"+deviceDescribe);
		
		List<tDevice> tdevice = tdeviceService.searchdeviceNo(deviceNo);
		
		String deviceId = tdevice.get(0).getDeviceId();
		
		List<tUser> tuser = tuserService.searchuserName(userName);
		
		String userId = tuser.get(0).getUserId();
		
		System.out.println("userId:"+userId);
		System.out.println("deviceId:"+deviceId);
		
		tUserAndtDevice tuserandtdevice = new tUserAndtDevice();
		
		tuserandtdevice.setUserId(userId);
		tuserandtdevice.setDeviceId(deviceId);
		
		uadservice.add(tuserandtdevice);
		
		
		return "success";
	}

	public String getDeviceNo() {
		return deviceNo;
	}

	public void setDeviceNo(String deviceNo) {
		this.deviceNo = deviceNo;
	}

	public String getDeviceName() {
		return deviceName;
	}

	public void setDeviceName(String deviceName) {
		this.deviceName = deviceName;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getDeviceDescribe() {
		return deviceDescribe;
	}

	public void setDeviceDescribe(String deviceDescribe) {
		this.deviceDescribe = deviceDescribe;
	}

	public tUserService getTuserService() {
		return tuserService;
	}

	public void setTuserService(tUserService tuserService) {
		this.tuserService = tuserService;
	}

	public tDeviceService getTdeviceService() {
		return tdeviceService;
	}

	public void setTdeviceService(tDeviceService tdeviceService) {
		this.tdeviceService = tdeviceService;
	}

	public tUserAndtDeviceService getUadservice() {
		return uadservice;
	}

	public void setUadservice(tUserAndtDeviceService uadservice) {
		this.uadservice = uadservice;
	}
	

}
